﻿
// libraryDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "library.h"
#include "libraryDlg.h"
#include "afxdialogex.h"

#include"inread.h"
#include"Seekread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// ClibraryDlg 对话框



ClibraryDlg::ClibraryDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_LIBRARY_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void ClibraryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST2, m_list);
}

BEGIN_MESSAGE_MAP(ClibraryDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &ClibraryDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &ClibraryDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &ClibraryDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &ClibraryDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &ClibraryDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &ClibraryDlg::OnBnClickedButton6)
	ON_BN_CLICKED(IDC_BUTTON7, &ClibraryDlg::OnBnClickedButton7)
	ON_BN_CLICKED(IDC_BUTTON8, &ClibraryDlg::OnBnClickedButton8)
	ON_BN_CLICKED(IDC_BUTTON9, &ClibraryDlg::OnBnClickedButton9)
END_MESSAGE_MAP()


// ClibraryDlg 消息处理程序

BOOL ClibraryDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);	// 整行选择、网格线
	m_list.InsertColumn(0, _T("读者ID"), LVCFMT_LEFT, 150);	// 插入第4列的列名
	m_list.InsertColumn(1, _T("姓名"), LVCFMT_LEFT, 150);	// 插入第2列的列名
	m_list.InsertColumn(2, _T("性别"), LVCFMT_LEFT, 150);	// 插入第3列的列名
	m_list.InsertColumn(3, _T("联系电话"), LVCFMT_LEFT, 150);	// 插入第4列的列名

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void ClibraryDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void ClibraryDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR ClibraryDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void ClibraryDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	if (SQL.Connect_MySQL())
	{
		read = SQL.Get_All_read(0);
		UpdateList();
	}
	else
	{
		return;
	}
}


void ClibraryDlg::OnBnClickedButton2()
{
	// TODO: 在此添加控件通知处理程序代码

	inread read1;
	if (read1.DoModal() == IDOK) {

		if (SQL.Add_read(Reader(read1.ID1.GetBuffer(), read1.姓名.GetBuffer(), read1.性别.GetBuffer(), read1.电话号码.GetBuffer()))) {
			MessageBox(TEXT("添加成功！"), TEXT("提示"));
			UpdateList();
		}
	}
}

void ClibraryDlg::UpdateList()
{
	m_list.DeleteAllItems();
	CString str;
	if (SQL.Connect_MySQL()) {
		read = SQL.Get_All_read(0);
		m_list.DeleteAllItems();

		for (int i = 0; i < read.size(); i++)
		{
			m_list.InsertItem(i, read[i].r_ID.c_str());
			m_list.SetItemText(i, 1, read[i].r_name.c_str());
			m_list.SetItemText(i, 2, read[i].r_sex.c_str());
			m_list.SetItemText(i, 3, read[i].r_phonenamber.c_str());
		}
	}
}


void ClibraryDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	int nIndex = m_list.GetSelectionMark();   //获取选中行的行号
	if (nIndex == -1)
	{
		MessageBox(TEXT("请先选择要删除的数据！"), TEXT("提示"));
		return;
	}
	else
	{
		UINT i;
		i = MessageBox(_T("确定要删除这条信息吗？"), _T("提示"), MB_YESNO | MB_ICONQUESTION);
		if (i == IDYES)
		{
			if (SQL.Del_read(read[nIndex]))
			{
				MessageBox(TEXT("删除成功！"), TEXT("提示"));
				read.erase(read.begin() + nIndex);
				UpdateList();
			}
		}
	}
}


void ClibraryDlg::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
	int nIndex = m_list.GetSelectionMark();   //获取选中行的行号
	if (nIndex == -1)
	{
		MessageBox(TEXT("请先选择要修改的数据！"), TEXT("提示"));
		return;
	}
	else
	{
		inread read1;
		read1.ID1 = read[nIndex].r_ID.c_str();
		read1.姓名 = read[nIndex].r_name.c_str();
		read1.性别 = read[nIndex].r_sex.c_str();
		read1.电话号码 = read[nIndex].r_phonenamber.c_str();
		if (read1.DoModal() == IDOK)
		{
			if (SQL.Update_read(read[nIndex], Reader(read1.ID1.GetBuffer(), read1.姓名.GetBuffer(), read1.性别.GetBuffer(), read1.电话号码.GetBuffer())))
			{
				MessageBox(TEXT("修改成功！"), TEXT("提示"));
				read[nIndex] = Reader(read1.ID1.GetBuffer(), read1.姓名.GetBuffer(), read1.性别.GetBuffer(), read1.电话号码.GetBuffer());
				UpdateList();
			}
		}
	}
}
void ClibraryDlg::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	Seekread read1;
	if (read1.DoModal() == IDOK)
	{
		vector<Reader> result = SQL.Seekread(read1.r_name);

		if (result.empty())
		{
			MessageBox(TEXT("查无此人！"), TEXT("提示"));
			return;
		}
		m_list.DeleteAllItems();
		CString str;
		if (SQL.Connect_MySQL()) {
			m_list.DeleteAllItems();
			for (int i = 0; i < result.size(); i++)
			{
				m_list.InsertItem(i, result[i].r_ID.c_str());
				m_list.SetItemText(i, 1, result[i].r_name.c_str());
				m_list.SetItemText(i, 2, result[i].r_sex.c_str());
				m_list.SetItemText(i, 3, result[i].r_phonenamber.c_str());
			}
		}
	}
}
void ClibraryDlg::OnBnClickedButton6()
{
	if (BR.DoModal() == IDOK) {
		if (SQL.borrow(BR.bor,BR.book,BR.jy)) {
			MessageBox(TEXT("借书成功！"), TEXT("提示"));
			UpdateList();
		}
	}

}
void ClibraryDlg::OnBnClickedButton7()
{
	// TODO: 在此添加控件通知处理程序代码
	if (re.DoModal() == IDOK) {
		if (SQL.return1(re.repe,re.bookshe)) {
			MessageBox(TEXT("还书成功！"), TEXT("提示"));
			UpdateList();
		}
	}
}



void ClibraryDlg::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
	if (sc.DoModal() == IDOK) {
		if (SQL.SS(sc.Sc)) {
			MessageBox(TEXT("删书成功！"), TEXT("提示"));
			UpdateList();
		}
	}

}

void ClibraryDlg::OnBnClickedButton9()
{
	// TODO: 在此添加控件通知处理程序代码
	if (wri.DoModal() == IDOK)
	{
		vector<Reader> result = SQL.WRI(wri.Wri);
		if (result.empty())
		{
			MessageBox(TEXT("您的输入有误！"), TEXT("提示"));
			return;
		}
		m_list.DeleteAllItems();
		CString str;
		if (SQL.Connect_MySQL()) {
			m_list.DeleteAllItems();
			for (int i = 0; i < result.size(); i++)
			{
				m_list.InsertItem(i, result[i].r_ID.c_str());
				m_list.SetItemText(i, 1, result[i].r_name.c_str());
				m_list.SetItemText(i, 2, result[i].r_sex.c_str());
				m_list.SetItemText(i, 3, result[i].r_phonenamber.c_str());
			}
		}
	}
}
